/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package posartenobsa;

import SQL.Insertar;
import com.sun.data.provider.impl.CachedRowSetDataProvider;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.sql.rowset.CachedRowSetXImpl;
import com.sun.webui.jsf.component.Alert;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.PasswordField;
import com.sun.webui.jsf.component.TextArea;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import javax.faces.FacesException;
import javax.faces.convert.IntegerConverter;
import javax.faces.event.ValueChangeEvent;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version A_Producto.java
 * @version Created on 20/03/2012, 08:24:55 PM
 * @author Fabio
 */

public class A_Producto extends AbstractPageBean {

    String PassDes, ProduDes;
    String NTipoP, NNombreP, NDescripP, PassMod, ProductoM;
    String Tipoproducto, Nombreproducto, Desproducto;
    String SQL;///
    Insertar in = new Insertar();
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        CB_NTipoDefaultOptions.setOptions(new com.sun.webui.jsf.model.Option[]{new com.sun.webui.jsf.model.Option("LANA", "LANA"), new com.sun.webui.jsf.model.Option("MADERA", "MADERA")});
        buscaproductoDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{A_Producto.buscaproductoRowSet}"));
        buscaproductoRowSet.setDataSourceName("java:comp/env/jdbc/public_PostgreSQL");
        buscaproductoRowSet.setCommand("SELECT idproducto, nombre_producto FROM producto WHERE producto_activo = TRUE");
        buscaproductoRowSet.setTableName("producto");
        }
    private SingleSelectOptionsList CB_NTipoDefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getCB_NTipoDefaultOptions() {
        return CB_NTipoDefaultOptions;
    }

    public void setCB_NTipoDefaultOptions(SingleSelectOptionsList ssol) {
        this.CB_NTipoDefaultOptions = ssol;
    }
    private CachedRowSetDataProvider buscaproductoDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getBuscaproductoDataProvider() {
        return buscaproductoDataProvider;
    }

    public void setBuscaproductoDataProvider(CachedRowSetDataProvider crsdp) {
        this.buscaproductoDataProvider = crsdp;
    }
    private CachedRowSetXImpl buscaproductoRowSet = new CachedRowSetXImpl();

    public CachedRowSetXImpl getBuscaproductoRowSet() {
        return buscaproductoRowSet;
    }

    public void setBuscaproductoRowSet(CachedRowSetXImpl crsxi) {
        this.buscaproductoRowSet = crsxi;
    }
    private DropDown CBTipoproducto = new DropDown();

    public DropDown getCBTipoproducto() {
        return CBTipoproducto;
    }

    public void setCBTipoproducto(DropDown dd) {
        this.CBTipoproducto = dd;
    }
    private TextField TF_Nombreproducto = new TextField();

    public TextField getTF_Nombreproducto() {
        return TF_Nombreproducto;
    }

    public void setTF_Nombreproducto(TextField tf) {
        this.TF_Nombreproducto = tf;
    }
    private TextArea TADescriP = new TextArea();

    public TextArea getTADescriP() {
        return TADescriP;
    }

    public void setTADescriP(TextArea ta) {
        this.TADescriP = ta;
    }
       
    private Alert alertaProGuardado = new Alert();

    public Alert getAlertaProGuardado() {
        return alertaProGuardado;
    }

    public void setAlertaProGuardado(Alert a) {
        this.alertaProGuardado = a;
    }
    private IntegerConverter CB_selProDConverter = new IntegerConverter();

    public IntegerConverter getCB_selProDConverter() {
        return CB_selProDConverter;
    }

    public void setCB_selProDConverter(IntegerConverter ic) {
        this.CB_selProDConverter = ic;
    }
    private IntegerConverter CB_selProMConverter = new IntegerConverter();

    public IntegerConverter getCB_selProMConverter() {
        return CB_selProMConverter;
    }

    public void setCB_selProMConverter(IntegerConverter ic) {
        this.CB_selProMConverter = ic;
    }
    private Alert alertaContraseñaProDes = new Alert();

    public Alert getAlertaContraseñaProDes() {
        return alertaContraseñaProDes;
    }

    public void setAlertaContraseñaProDes(Alert a) {
        this.alertaContraseñaProDes = a;
    }
    private Alert alertaProduDesactivado = new Alert();

    public Alert getAlertaProduDesactivado() {
        return alertaProduDesactivado;
    }

    public void setAlertaProduDesactivado(Alert a) {
        this.alertaProduDesactivado = a;
    }
    private PasswordField TFPassProD = new PasswordField();

    public PasswordField getTFPassProD() {
        return TFPassProD;
    }

    public void setTFPassProD(PasswordField pf) {
        this.TFPassProD = pf;
    }
    private DropDown CB_selProD = new DropDown();

    public DropDown getCB_selProD() {
        return CB_selProD;
    }

    public void setCB_selProD(DropDown dd) {
        this.CB_selProD = dd;
    }
    private DropDown CB_selProM = new DropDown();

    public DropDown getCB_selProM() {
        return CB_selProM;
    }

    public void setCB_selProM(DropDown dd) {
        this.CB_selProM = dd;
    }
    private DropDown CB_NTipo = new DropDown();

    public DropDown getCB_NTipo() {
        return CB_NTipo;
    }

    public void setCB_NTipo(DropDown dd) {
        this.CB_NTipo = dd;
    }
    private TextField TF_NNombreP = new TextField();

    public TextField getTF_NNombreP() {
        return TF_NNombreP;
    }

    public void setTF_NNombreP(TextField tf) {
        this.TF_NNombreP = tf;
    }
    private TextArea TA_NDescripcionP = new TextArea();

    public TextArea getTA_NDescripcionP() {
        return TA_NDescripcionP;
    }

    public void setTA_NDescripcionP(TextArea ta) {
        this.TA_NDescripcionP = ta;
    }
    private PasswordField passMPro = new PasswordField();

    public PasswordField getPassMPro() {
        return passMPro;
    }

    public void setPassMPro(PasswordField pf) {
        this.passMPro = pf;
    }
    private Alert alertaModificadoPro = new Alert();

    public Alert getAlertaModificadoPro() {
        return alertaModificadoPro;
    }

    public void setAlertaModificadoPro(Alert a) {
        this.alertaModificadoPro = a;
    }
    private Alert alertaContraseñaMPro = new Alert();

    public Alert getAlertaContraseñaMPro() {
        return alertaContraseñaMPro;
    }

    public void setAlertaContraseñaMPro(Alert a) {
        this.alertaContraseñaMPro = a;
    }

    // </editor-fold>


    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public A_Producto() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("A_Producto Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
        buscaproductoDataProvider.close();
   }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    public String HPL_AUsuario1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "case1";
    }

    public String HPL_ASocio_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "case2";
    }

    public String hyperlink2_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }

    public String HPL_ADeposito_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "case3";
    }

    public String HPL_AExposicion1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "case4";
    }

    public String HPL_Vender_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "case5";
    }

    public String tabCUsuario_action() {
        alertaProGuardado.setVisible(false);
        return null;
    }

    public String b_Guardarusuario_action() {
        Nombreproducto=(String) TF_Nombreproducto.getText();
        Tipoproducto= (String)CBTipoproducto.getValue();
        Desproducto= (String) TADescriP.getText();
        TF_Nombreproducto.setText(null);
        TADescriP.setText(null);
        SQL = "INSERT INTO producto (nombre_producto, tipo_producto, producto_activo, descripcion, cantidad)"
                +"VALUES ('" + Nombreproducto + "','" + Tipoproducto + "',TRUE,'" + Desproducto+ "',0);";
        in.insertar(SQL);
        alertaProGuardado.setVisible(true);
        return null;
    }

    public String tabMSocio_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }

    

    public String tabDSocio_action() {
        alertaContraseñaProDes.setVisible(false);
        alertaProduDesactivado.setVisible(false); 
        return null;
    }

    public String b_Desactivarusuario1_action() {
       PassDes=(String) TFPassProD.getText();
       ProduDes= String.valueOf(CB_selProD.getValue());
       System.out.println(ProduDes);
       if (PassDes.equals("Fabio")){

           SQL = "UPDATE producto SET producto_activo = FALSE WHERE idproducto ='" + ProduDes + "';";
        in.insertar(SQL);
        buscaproductoDataProvider.refresh();
        alertaContraseñaProDes.setVisible(false);
        alertaProduDesactivado.setVisible(true);
       }
 else{
        alertaContraseñaProDes.setVisible(true);
        alertaProduDesactivado.setVisible(false);
 }


        return null;
    }

    public String alertaProGuardado_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }

    public String alertaGuardado2_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }

    public String b_Modificarproducto_action() {

        NTipoP = (String) CB_NTipo.getValue();
        NNombreP = (String) TF_NNombreP.getText();
        NDescripP = (String) TA_NDescripcionP.getText();
        PassMod = (String) passMPro.getText();
        ProductoM= String.valueOf(CB_selProM.getValue());
        if(PassMod.equals("Fabio")){
           
        if (NNombreP != null) {
                SQL = "UPDATE producto SET nombre_producto = '" + NNombreP + "' WHERE idproducto =" + ProductoM + ";";
                in.insertar(SQL);
                buscaproductoDataProvider.refresh();
            }

        if (NTipoP != null) {
                SQL = "UPDATE producto SET tipo_producto = '" + NTipoP + "' WHERE idproducto =" + ProductoM + ";";
                in.insertar(SQL);
                buscaproductoDataProvider.refresh();
            }

        if (NDescripP != null) {
                SQL = "UPDATE producto SET descripcion_producto = '" + NTipoP + "' WHERE idproducto =" + ProductoM + ";";
                in.insertar(SQL);
                buscaproductoDataProvider.refresh();
            }

        TF_NNombreP.setText(null);
        TA_NDescripcionP.setText(null);
        alertaContraseñaMPro.setVisible(false);
        alertaModificadoPro.setVisible(true);

        }
 else {
        alertaContraseñaMPro.setVisible(true);
        alertaModificadoPro.setVisible(false);

 }

        return null;
    }

    public void TA_NDescripcionP_processValueChange(ValueChangeEvent event) {
    }
    
}

